﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_Member_SetMemPosition : System.Web.UI.Page
{
    DataView dv;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetAllUsers();
            dt = (DataTable)userTable;


            SQLExpressDAL.Users.UsersDataTable singleUserTable = userAdp.GetSingleUserInfo(Session["UserID"].ToString());
            DataRow row = singleUserTable.Rows[0];

            //创建根节点 
            TreeNode root = new TreeNode();
            //必须与数据库的对应 
            root.Text = string.Format("| [{0}] | {1} | {2} | {3}", row["userid"].ToString(), row["username"].ToString(), row["LjScore"].ToString(), row["SpecialScore"].ToString());
            root.Value = row["userid"].ToString();
            //添加根节点 
            this.TreeView1.Nodes.Add(root);

            //第一次加载时调用方法传参 
            CreateTree(Session["UserID"].ToString(), root, dt, this.TreeView1);
            this.TreeView1.ExpandAll();
        }
    }

    /// <summary> 
    /// 创建一个树 
    /// </summary> 
    /// <param name="parentID">父ID</param> 
    /// <param name="node">节点</param> 
    /// <param name="dt">DataTable</param> 
    /// <param name="treeView">TreeView的名称</param> 
    public void CreateTree(String parentID, TreeNode node, DataTable dt, TreeView treeView)
    {
        //实例化一个DataView dt = 传入的DataTable 
        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [cdefine1]='" + parentID + "'";
        string template = "[A]| [{0}] | {1} | {2} | {3}";
        string ljScore = "";
        string specialScore = "";
        //用foreach遍历dv 
        foreach (DataRowView row in dv)
        {
            //第一次加载时为空 
            if (node == null)
            {
                //创建根节点 
                TreeNode root = new TreeNode();
                //必须与数据库的对应 
                ljScore = row["LjScore"].ToString();
                ljScore = string.IsNullOrEmpty(ljScore) ? "0.00" : ljScore;

                specialScore = row["SpecialScore"].ToString();
                specialScore = string.IsNullOrEmpty(specialScore) ? "0.00" : specialScore;

                root.Text = string.Format(template, row["userid"].ToString(), row["username"].ToString(), ljScore, specialScore);
                root.Value = row["userid"].ToString();
                //添加根节点 
                this.TreeView1.Nodes.Add(root);
                //递归调用方法本身 
                CreateTree(row["userid"].ToString(), root, dt, treeView);
                template = "[B] | [{0}] | {1}| {2}|  {3}";
            }
            else
            {
                //添加子节点 
                TreeNode childNode = new TreeNode();
                ljScore = row["LjScore"].ToString();
                ljScore = string.IsNullOrEmpty(ljScore) ? "0.00" : ljScore;

                specialScore = row["SpecialScore"].ToString();
                specialScore = string.IsNullOrEmpty(specialScore) ? "0.00" : specialScore;

                childNode.Text = childNode.Text = string.Format(template, row["userid"].ToString(), row["username"].ToString(), ljScore, specialScore);
                childNode.Value = row["userid"].ToString();
                node.ChildNodes.Add(childNode);
                CreateTree(row["userid"].ToString(), childNode, dt, treeView);
                template = "[B] | [{0}] | {1} |{2} | {3}";
              
                   if( childNode.ChildNodes.Count==2)
                   {
                       childNode.ToolTip = "full";
                   } 
            }
        }

    }


    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        string selectedTxt = this.TreeView1.SelectedNode.Value;
        Response.Write(string.Format("<script>window.opener.document.getElementById('{0}').value ='{1}';window.close();</script>", Request["a"], selectedTxt));
    }
}
